package com.ac.controller;

import java.util.List;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ac.aop.Auth;
import com.ac.model.AuthMenu;
import com.ac.response.Res;
import com.ac.service.AuthMenuService;
import com.ac.service.AuthTokenService;
import com.ac.service.AuthUserService;
import com.ac.util.StrUtils;

import lombok.AllArgsConstructor;

@RestController
@RequestMapping("auth/menu")
@AllArgsConstructor
public class AuthMenuController {

	private final AuthMenuService MenuService;
	private final AuthUserService UserService;
	private final AuthTokenService ts;

	@Auth
	@PostMapping(value = "list")
	public Res list(@RequestHeader("token") String token) {
		List<AuthMenu> users = MenuService.list();
		Res res = Res.success(null, users, users.size());
		return ts.getToken(res, token);
	}

	@Auth
	@PostMapping(value = "save")
	public Res save(@RequestBody AuthMenu menu, @RequestHeader("token") String token) {
		if (StrUtils.isEmpty(menu.getId()))
			MenuService.insert(menu);
		else
			MenuService.update(menu);
		Res res = Res.success("保存成功");
		return ts.getToken(res, token);
	}

	@Auth
	@PostMapping(value = "remove")
	public Res remove(@RequestBody AuthMenu menu, @RequestHeader("token") String token) {
		MenuService.remove(menu);
		Res res = Res.success("删除成功");
		return ts.getToken(res, token);
	}

	/**
	 * 废弃
	 * @param token
	 * @return
	 */
	@Auth
	@PostMapping(value = "menuList")
	public Res menuList(@RequestHeader("token") String token) {
		List<AuthMenu> users = MenuService.menuList(UserService.info(ts.getUserId(token),true));
		Res res = Res.success(null, users, users.size());
		return ts.getToken(res, token);
	}

}
